package com.py.net.coder;

import com.google.protobuf.MessageLite;
import com.py.net.proto.ProtoMapper;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class ProtoToByteEncoder extends MessageToByteEncoder<MessageLite> {

	@Override
	protected void encode(ChannelHandlerContext ctx, MessageLite msg, ByteBuf out) throws Exception {
		// proto msg编码成byteBuf
		byte[] data = msg.toByteArray();
		out.writeInt(ProtoMapper.getMessageType(msg.getClass()));
		out.writeInt(data.length);
		out.writeBytes(data);
	}

}
